package com.woniu.outpatient.service.impl;

import java.math.BigDecimal;
import java.util.*;

import com.woniu.outpatient.domain.MoreRegistration;
import com.woniu.outpatient.service.UserDeptAndDoctorService;
import com.woniu.user.domain.DeptAndDoctorName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.woniu.outpatient.mapper.RegistrationMapper;
import com.woniu.outpatient.domain.Registration;
import com.woniu.outpatient.service.IRegistrationService;

import javax.annotation.Resource;

/**
 * 挂号流水表Service业务层处理
 *
 * @author wolf-six
 * @date 2023-02-15
 */
@Service
public class RegistrationServiceImpl implements IRegistrationService
{

    @Autowired
   private UserDeptAndDoctorService userDeptAndDoctorService;
    @Resource
    private RegistrationMapper registrationMapper;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 查询挂号流水表
     *
     * @param registrationId 挂号流水表主键
     * @return 挂号流水表
     */
    @Override
    public Registration selectRegistrationByRegistrationId(Long registrationId)
    {
        return registrationMapper.selectRegistrationByRegistrationId(registrationId);
    }

    /**
     * 查询挂号流水表列表
     *
     * @param registration 挂号流水表
     * @return 挂号流水表
     */
    @Override
    public List<Registration> selectRegistrationList(Registration registration)
    {
        return registrationMapper.selectOkPayMoreRegistrationList(registration);
    }

    /**
     * 新增挂号流水表
     *
     * @param registration 挂号流水表
     * @return 结果
     */
    @Override
    public int insertRegistration(Registration registration)
    {
        registration.setRegistrationStarttime(new Date());
        registration.setState("已缴费");
        registration.setDeptId(4l);
        registration.setDoctorId(1l);
        return registrationMapper.insertRegistration(registration);
    }

    /**
     * 修改挂号流水表
     *
     * @param registration 挂号流水表
     * @return 结果
     */
    @Override
    public int updateRegistration(Registration registration)
    {
        return registrationMapper.updateRegistration(registration);
    }

    public int updateRegistrationStatus(Registration registration){
        registration.setState("已接诊");
        return registrationMapper.updateRegistration(registration);
    }

    /**
     * 已缴费挂号队列
     * @param registration
     * @return
     */
    @Override
    public List<MoreRegistration> selectOkPayMoreRegistrationList(Registration registration) {

        List<Registration> registrations = registrationMapper.selectOkPayMoreRegistrationList(registration);
        List<MoreRegistration> list = new ArrayList<>();
        int count=1;
        for (Registration registration1 : registrations) {
            Long doctorId = registration1.getDoctorId();
            DeptAndDoctorName doctorNameAndDeptName = userDeptAndDoctorService.getDoctorNameAndDeptName(doctorId);

            MoreRegistration m = new MoreRegistration();
            m.setRegistrationId(registration1.getRegistrationId());
            m.setPatientName(registration1.getPatientName());
            m.setDickerSex(registration1.getDickerSex());
            m.setPatientIdcard(registration1.getPatientIdcard());
            m.setPatientAge(registration1.getPatientAge());
            m.setDoctorTel(registration1.getDoctorTel());
            m.setDoctorAdd(registration1.getDoctorAdd());
            m.setRegistrationStarttime(registration1.getRegistrationStarttime());
            m.setRegistrationEndtime(registration1.getRegistrationEndtime());
            m.setRegistrationMoney(registration1.getRegistrationMoney());
            m.setDoctorName(doctorNameAndDeptName.getDoctorName());
            m.setDeptName(doctorNameAndDeptName.getDeptName());
            m.setState(registration1.getState());
            m.setSortNum(count);
            count++;
            list.add(m);
        }
        return list;
    }

    /**
     * 不包含未缴费挂号队列
     * @param registration
     * @return
     */
    @Override
    public List<MoreRegistration> selectPendingDiagnosisList(Registration registration) {
        List<Registration> registrations = registrationMapper.selectPendingDiagnosisList(registration);
        List<MoreRegistration> list = new ArrayList<>();
        int count=1;
        for (Registration registration1 : registrations) {
            Long doctorId = registration1.getDoctorId();
            DeptAndDoctorName doctorNameAndDeptName = userDeptAndDoctorService.getDoctorNameAndDeptName(doctorId);

            MoreRegistration m = new MoreRegistration();
            m.setRegistrationId(registration1.getRegistrationId());
            m.setPatientName(registration1.getPatientName());
            m.setDickerSex(registration1.getDickerSex());
            m.setPatientIdcard(registration1.getPatientIdcard());
            m.setPatientAge(registration1.getPatientAge());
            m.setDoctorTel(registration1.getDoctorTel());
            m.setDoctorAdd(registration1.getDoctorAdd());
            m.setRegistrationStarttime(registration1.getRegistrationStarttime());
            m.setRegistrationEndtime(registration1.getRegistrationEndtime());
            m.setRegistrationMoney(registration1.getRegistrationMoney());
            m.setDoctorName(doctorNameAndDeptName.getDoctorName());
            m.setDeptName(doctorNameAndDeptName.getDeptName());
            m.setState(registration1.getState());
            m.setSortNum(count);
            count++;
            list.add(m);
        }
        return list;
    }

    @Override
    public List<BigDecimal> getEchartsList() {
        return registrationMapper.getEchartsList();
    }

    /**
     * 批量删除挂号流水表
     *
     * @param registrationIds 需要删除的挂号流水表主键
     * @return 结果
     */
    @Override
    public int deleteRegistrationByRegistrationIds(Long[] registrationIds)
    {
        return registrationMapper.deleteRegistrationByRegistrationIds(registrationIds);
    }

    /**
     * 删除挂号流水表信息
     *
     * @param registrationId 挂号流水表主键
     * @return 结果
     */
    @Override
    public int deleteRegistrationByRegistrationId(Long registrationId)
    {
        return registrationMapper.deleteRegistrationByRegistrationId(registrationId);
    }

    @Override
    public List<MoreRegistration> selectMoreRegistrationList(Registration registration) {
        List<Registration> registrations = registrationMapper.selectRegistrationList(registration);
        List<MoreRegistration> list = new ArrayList<>();
        int count=1;
        for (Registration registration1 : registrations) {
            Long doctorId = registration1.getDoctorId();
            DeptAndDoctorName doctorNameAndDeptName = userDeptAndDoctorService.getDoctorNameAndDeptName(doctorId);
            MoreRegistration m = new MoreRegistration();
            m.setRegistrationId(registration1.getRegistrationId());
            m.setPatientName(registration1.getPatientName());
            m.setDickerSex(registration1.getDickerSex());
            m.setPatientIdcard(registration1.getPatientIdcard());
            m.setPatientAge(registration1.getPatientAge());
            m.setDoctorTel(registration1.getDoctorTel());
            m.setDoctorAdd(registration1.getDoctorAdd());
            m.setRegistrationStarttime(registration1.getRegistrationStarttime());
            m.setRegistrationEndtime(registration1.getRegistrationEndtime());
            m.setRegistrationMoney(registration1.getRegistrationMoney());
            m.setDoctorName(doctorNameAndDeptName.getDoctorName());
            m.setDeptName(doctorNameAndDeptName.getDeptName());
            m.setState(registration1.getState());
            m.setSortNum(count);
            count++;
            list.add(m);
        }

        return list;
    }




    @Override
    public List<Registration> selAllListToSortByStatus() {
        return registrationMapper.selAllListToSortByStatus();
    }


    /**
     * 多表拼接查询挂号表
     * @param registration
     * @return
     */

}
